package com.offcn.sellergood.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.offcn.entity.PageResult;
import com.offcn.sellergood.dao.SpecificationMapper;
import com.offcn.sellergood.dao.SpecificationOptionMapper;
import com.offcn.sellergood.entity.SpecificationEntity;
import com.offcn.sellergood.pojo.Specification;
import com.offcn.sellergood.pojo.SpecificationOption;
import com.offcn.sellergood.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/****
 * @Author:ujiuye
 * @Description:Specification业务层接口实现类
 * @Date 2021/2/1 14:19
 *****/
@Service
@Transactional
public class SpecificationServiceImpl extends ServiceImpl<SpecificationMapper,Specification> implements SpecificationService {

    @Autowired
    private SpecificationOptionMapper optionMapper;

    /**
     * @param: Long id
     * @return: SpecificationEntity specificationEntity(specification,options)
     * @description: 查询：先查询规格，在根据id来查询规格选项
     * 主键查询：T getById(Serializable id)：根据 ID 查询
     * List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
     * selectList：条件构造器查询——所有记录，用实体类封装对象
     */
    @Override
    public SpecificationEntity findById(Long id) {
        Specification specification = this.getById(id); // 获取规格
        QueryWrapper<SpecificationOption> wrapper = new QueryWrapper<>();
        wrapper.eq("spec_id",id);
        List<SpecificationOption> options = optionMapper.selectList(wrapper);
        return new SpecificationEntity(specification,options);
    }

    /**
     * @param: SpecificationEntity specificationEntity(specification,options)
     * @return: null
     * @description: 添加：插入规格返回规格ID，然后循环插入规格选项
     * boolean save(T entity): 插入单条数据，实际调用的是BaseMapper的insert
     * int insert(T entity): 直接添加
     */
    @Override
    public void insert(SpecificationEntity specificationEntity) {
        Specification specification = specificationEntity.getSpecification();
        this.save(specification); // 将规则添加
        Long specificationId = specification.getId();

        List<SpecificationOption> options = specificationEntity.getOptions();
        if(options != null && options.size() > 0){
            for (SpecificationOption option : options) {
                option.setSpecId(specificationId); // 将specificationId添加到实体类对象中
                optionMapper.insert(option);
            }
        }
    }

    /**
     * @param: SpecificationEntity specificationEntity
     * @return: null
     * @description: 修改：修改规格返回规格ID，根据规格ID，先删除，然后循环插入规格选项
     * boolean updateById(T entity)：根据 ID 选择修改
     * int delete(@Param("ew") Wrapper<T> queryWrapper):条件构造器删除
     */
    @Override
    public void update(SpecificationEntity specificationEntity) {
        Specification specification = specificationEntity.getSpecification();
        this.updateById(specification); // 修改规格
        Long specificationId = specification.getId(); // 根据规格id，先删除选项
        QueryWrapper<SpecificationOption> wrapper = new QueryWrapper<>();
        wrapper.eq("spec_id",specificationId);
        optionMapper.delete(wrapper);
        // 将规则选项重新添加进来
        List<SpecificationOption> options = specificationEntity.getOptions();
        if(!CollectionUtils.isEmpty(options) && options.size() > 0){
            for (SpecificationOption option : options) {
                option.setSpecId(specificationId);
                optionMapper.insert(option);
            }
        }
    }

    /**
     * @param: Long id
     * @return: null
     * @description: 删除：先删除规格，在根据id来删除规格选项
     *  boolean removeById(Serializable id)：根据 ID 删除 实际调用的是BaseMapper中的相关delete方法
     *  int delete(@Param("ew") Wrapper<T> queryWrapper):条件构造器删除
     */
    @Override
    public void delete(Long id) {
        this.removeById(id); // 删除规则
        QueryWrapper<SpecificationOption> wrapper = new QueryWrapper<>();
        wrapper.eq("spec_id",id);
        optionMapper.delete(wrapper); // 删除规格选项
    }

    /**
     * @param: null
     * @return: List<Map>
     * @description: 查询规格下拉列表
     */
    @Override
    public List<Map> selectOptions() {
        return optionMapper.selectOptions();
    }
}
